﻿@{
    ViewBag.Title = "AddWorkstation";
    Layout = null;
}

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Demo</title>

    <link href="//unpkg.com/layui@2.9.17/dist/css/layui.css" rel="stylesheet">
    <link href="~/layui/css/layui.css" rel="stylesheet" />
    <script src="~/layui/layui.js"></script>
</head>
<body>
    <form class="layui-form" action="" onsubmit="return false">
        <div class="layui-form-item">
            <label class="layui-form-label">工作站编码</label>
            <div class="layui-input-block">
                <input type="text" name="workstationCode" id="workstationCode" lay-verify="required" placeholder="请输入" autocomplete="off" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">工作站名称</label>
            <div class="layui-input-block">
                <input type="text" name="workstationName" id="workstationName" lay-verify="required" placeholder="请输入" autocomplete="off" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">工作站地点</label>
            <div class="layui-input-block">
                <input type="text" name="address" id="address" lay-verify="required" placeholder="请输入" autocomplete="off" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">所在车间名称</label>
            <div class="layui-input-inline layui-input-wrap">
                <select name="workShopCode" id="workShopId" lay-verify="required" lay-search>
                    <option value="">请选择车间</option>
                </select>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">所属工序</label>
            <div class="layui-input-inline layui-input-wrap">
                <select name="processCode" id="processId" lay-verify="required" lay-search>
                    <option value="">请选择工序</option>
                </select>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">是否启用</label>
            <div class="layui-input-block">
                <input type="radio" name="isEnable" value="1" title="是" checked>
                <input type="radio" name="isEnable" value="0" title="否">
            </div>
        </div>

        <div class="layui-form-item layui-form-text">
            <label class="layui-form-label">备注</label>
            <div class="layui-input-block">
                <textarea placeholder="请输入内容" class="layui-textarea" name="remark"></textarea>
            </div>
        </div>
    </form>

    <script src="~/libs/jquery/jquery.js"></script>
    <script src="//unpkg.com/layui@2.9.16/dist/layui.js"></script>

    <script type="text/javascript">
        layui.use('form',function(){
            var form = layui.form;
            form.render();

            // 自定义验证规则
            form.verify({
                workstationCode: function (value) {
                    if (!value) { layer.msg("工作站编码不能为空"); return true; };
                },
                workstationName: function (value) {
                    if (!value) { layer.msg("工作站名称不能为空"); return true; };
                },
                address: function (value) {
                    if (!value) { layer.msg("工作站地点不能为空"); return true; };
                },
                remark: function (value) {
                    if (!value) { layer.msg("车间备注不能为空"); return true; };
                }
            });

            var workstationCode = generateRandomNumber();
            form.on('switch(switchTest)', function (data) {
                var elem = data.elem; // 得到开关原始DOM对象
                var value = elem.checked; // 开关是否开启，true或者false
                var inputElem = document.getElementById('workstationCode');
                if (value == true) {
                    inputElem.value = generateRandomNumber();// 根据开关状态给input赋值
                }
                else {
                    inputElem.value = " ";
                }
            })
            //随机数
            function generateRandomNumber() {
                var num = Math.floor(1000 + Math.random() * 9000); // 生成1000到9999之间的随机数
                var number = "WS" + num
                return number;
            }

            //动态绑定车间
            $(document).ready(function () {
                $.ajax({
                    url: 'https://localhost:44311/api/app/workstation/work-shop',
                    type: 'GET',
                    dataType: 'json',
                    success: function (data) {
                        var html = '';
                        $.each(data.data, function (i, item) {
                            console.log(item);
                            html += '<option value="' + item.WorkShopCode + '">' + item.WorkShopName + '</option>';
                        });
                        $("#workShopId").append(html);
                        form.render('select');
                    }
                })
            })

            //动态绑定工序
            $(document).ready(function () {
                $.ajax({
                    url: 'https://localhost:44311/api/app/workstation/pro-process',
                    type: 'GET',
                    dataType: 'json',
                    success: function (data) {
                        var html = '';
                        $.each(data.data, function (i, item) {
                            html += '<option value="' + item.ProcessCode + '">' + item.ProcessName + '</option>';
                        });
                        $("#processId").append(html);
                        form.render('select');
                    }
                })
            })
        })
    </script>
</body>
</html>